package com.ruicar.afs.cloud.afscase.loanapprove.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.afscase.channel.entity.ChannelBaseInfo;
import com.ruicar.afs.cloud.afscase.channel.entity.ChannelCoopeDrawerParty;
import com.ruicar.afs.cloud.afscase.channel.entity.DrawerParty;
import com.ruicar.afs.cloud.afscase.channel.service.ChannelBaseInfoService;
import com.ruicar.afs.cloud.afscase.channel.service.ChannelCoopeDrawerPartyService;
import com.ruicar.afs.cloud.afscase.channel.service.DrawerPartyService;
import com.ruicar.afs.cloud.afscase.infomanagement.entity.CaseCarInfo;
import com.ruicar.afs.cloud.afscase.infomanagement.entity.CaseChannelInfo;
import com.ruicar.afs.cloud.afscase.infomanagement.entity.CaseContractInfo;
import com.ruicar.afs.cloud.afscase.infomanagement.service.CaseCarInfoService;
import com.ruicar.afs.cloud.afscase.infomanagement.service.CaseChannelInfoService;
import com.ruicar.afs.cloud.afscase.infomanagement.service.CaseContractInfoService;
import com.ruicar.afs.cloud.afscase.loanapprove.condition.InvoiceInspectionCondition;
import com.ruicar.afs.cloud.afscase.loanapprove.entity.CarInvoiceInfo;
import com.ruicar.afs.cloud.afscase.loanapprove.mapper.CarInvoiceInfoMapper;
import com.ruicar.afs.cloud.afscase.loanapprove.service.CarInvoiceInfoService;
import com.ruicar.afs.cloud.afscase.loanapprove.vo.InvoiceInspectionVo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>Description：车辆发票信息实现类</p>
 *
 * @author xueyu.ma
 * @version 1.0
 * @createDate 2020-6-15
 */
@Slf4j
@Data
@AllArgsConstructor
@Service
public class CarInvoiceInfoServiceImpl extends ServiceImpl<CarInvoiceInfoMapper, CarInvoiceInfo> implements CarInvoiceInfoService {

    private CaseChannelInfoService caseChannelInfoService;

    private CaseContractInfoService caseContractInfoService;

    private ChannelBaseInfoService channelBaseInfoService;

    private DrawerPartyService drawerPartyService;

    private ChannelCoopeDrawerPartyService channelCoopeDrawerPartyService;

    private CaseCarInfoService caseCarInfoService;
    /**
     * 查询二次发票验证结果
     *
     * @param page
     * @param condition
     * @return
     */
    @Override
    public IPage<InvoiceInspectionVo> getInvoiceList(Page page, InvoiceInspectionCondition condition) {
        return baseMapper.getInvoiceList(page,condition);
    }

    /**
     * 入库开票方端
     *
     * @param caseContractNo
     * @Author rongji.zhang
     * @Date 2020/09/21 17:38
     */
    @Override
    public void saveDrawerParty(String caseContractNo) {
        try {
            CarInvoiceInfo carInvoiceInfo = this.getOne(Wrappers.<CarInvoiceInfo>query().lambda()
                    .eq(CarInvoiceInfo::getContractNo,caseContractNo),false);
            CaseContractInfo caseContractInfo = caseContractInfoService.getOne(Wrappers.<CaseContractInfo>query().lambda()
                    .eq(CaseContractInfo::getContractNo,caseContractNo),false);
            if(!ObjectUtils.isEmpty(caseContractInfo)){
                //获取案件合作商关联信息
                CaseChannelInfo caseChannelInfo = caseChannelInfoService.getOne(Wrappers.<CaseChannelInfo>query().lambda()
                        .eq(CaseChannelInfo::getApplyNo,caseContractInfo.getApplyNo()));
                if(!ObjectUtils.isEmpty(caseChannelInfo)){
                    //获取合作商信息
                    ChannelBaseInfo channelBaseInfo = channelBaseInfoService.getOne(Wrappers.<ChannelBaseInfo>query().lambda()
                            .eq(ChannelBaseInfo::getChannelCode,caseChannelInfo.getDealerNo()));
                    if(!ObjectUtils.isEmpty(channelBaseInfo)){
                        List<DrawerParty> drawerPartyList = drawerPartyService.list(Wrappers.<DrawerParty>query().lambda()
                                .eq(DrawerParty::getDrawerPartyName,carInvoiceInfo.getMakeInvoiceUnit()));
                        if(CollectionUtils.isNotEmpty(drawerPartyList)){
                            drawerPartyList.forEach(drawerParty -> {
                                List<ChannelCoopeDrawerParty> channelCoopeDrawerPartyList = channelCoopeDrawerPartyService.list(Wrappers.<ChannelCoopeDrawerParty>query().lambda()
                                        .eq(ChannelCoopeDrawerParty::getDrawerId,drawerParty.getId())
                                        .eq(ChannelCoopeDrawerParty::getChannelId,channelBaseInfo.getId())
                                        .eq(StringUtils.isNotBlank(caseChannelInfo.getCarDealersId()),ChannelCoopeDrawerParty::getCardealerId,caseChannelInfo.getCarDealersId()));
                                if(CollectionUtils.isEmpty(channelCoopeDrawerPartyList)){
                                    ChannelCoopeDrawerParty channelCoopeDrawerParty = new ChannelCoopeDrawerParty();
                                    channelCoopeDrawerParty.setDrawerId(drawerParty.getId());
                                    channelCoopeDrawerParty.setChannelId(channelBaseInfo.getId());
                                    channelCoopeDrawerParty.setCardealerId(Long.valueOf(caseChannelInfo.getCarDealersId()));
                                    channelCoopeDrawerPartyService.save(channelCoopeDrawerParty);
                                }
                            });
                        } else {
                            DrawerParty newDrawerParty = new DrawerParty();
                            newDrawerParty.setDrawerPartyName(carInvoiceInfo.getMakeInvoiceUnit());
                            drawerPartyService.save(newDrawerParty);
                            ChannelCoopeDrawerParty channelCoopeDrawerParty = new ChannelCoopeDrawerParty();
                            channelCoopeDrawerParty.setDrawerId(newDrawerParty.getId());
                            channelCoopeDrawerParty.setChannelId(channelBaseInfo.getId());
                            if(StringUtils.isNotBlank(caseChannelInfo.getCarDealersId())){
                                channelCoopeDrawerParty.setCardealerId(Long.valueOf(caseChannelInfo.getCarDealersId()));
                            }
                            channelCoopeDrawerPartyService.save(channelCoopeDrawerParty);
                        }
                    }
                }
            }
        }catch (Exception e){
            log.info("入库开票方端信息失败！",e);
        }
    }

    /**
     * 查询所有二次发票验证结果
     *
     * @param condition
     * @return
     */
    @Override
    public List<InvoiceInspectionVo> getAllResultList(InvoiceInspectionCondition condition) {
        return baseMapper.getAllResultList(condition);
    }
}
